package jp.tuyano.helo.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class Pick implements EntryPoint {

    private Label label;
    private FlexTable resultPanel;
    private Label resultLabel = new Label();
    
    private static final String SERVER_ERROR = "SERVER ERROR!!";
    
    private final PickPDServiceAsync pickService =
            GWT.create(PickPDService.class);
    
    public void onModuleLoad() {
        
        label = new Label("please input search condition(name).");
        RootPanel.get("pick_label").add(label);
        RootPanel.get("result_label").add(resultLabel);
        final TextBox name = new TextBox();
        RootPanel.get("pick_panel").add(name);
        Button sendButton = new Button("search");
        sendButton.addClickHandler(new ClickHandler() {
            
            public void onClick(ClickEvent event) {
                
                sendToServer(name.getText());
            }
        });
        RootPanel.get("pick_button").add(sendButton);
        
    }

    public void sendToServer(String condition) {
        pickService.pickEntity(condition, new AsyncCallback<List<MyDataObject>>() {

            public void onSuccess(List<MyDataObject> result) {
                
                if (!result.isEmpty()) {
                    FlexTable table = new FlexTable();
                    table.setBorderWidth(1);
                    table.setText(0, 0, "ID");
                    table.setText(0, 1, "name");
                    table.setText(0, 2, "mail");
                    table.setText(0, 3, "tel");
                    for (int i = 0; i < result.size(); i++) {
                        MyDataObject data = result.get(i);
                        table.setText(i+1, 0,
                                String.valueOf(data.getId()));
                        table.setText(i+1, 1, data.getName());
                        table.setText(i+1, 2, data.getMail());
                        table.setText(i+1, 3, data.getTel());
                    }
                    if (resultPanel != null) {
                        RootPanel.get("result_panel").remove(resultPanel);
                    }
                    resultPanel = table;
                    resultLabel.setText("Search Result.");
                } else {
                    if (resultPanel != null) {
                        RootPanel.get("result_panel").remove(resultPanel);
                    }
                    resultPanel = new FlexTable();
                    resultLabel.setText("Not Exist.");
                }
                RootPanel.get("result_panel").add(resultPanel);
            }

            public void onFailure(Throwable caught) {

                resultLabel.setText(SERVER_ERROR);
            }
        });
    }
}
